import json
import requests


def ssoLogin(ip, port):
    headers = {'Content-Type': 'application/json'}
    verify = f'http://{ip}:{port}/oss/common/verify'
    response_verity = requests.get(verify, headers=headers)
    uuid = json.loads(response_verity.text)['data']['uuid']
    sso_login = f'http://{ip}:{port}/oss/login'
    data_login = {"account": "hosZw0B8+HqRSy7qfhr3tQ==", "password": "WACX9SvxvHOpPjnow6t0Ug==", "code": "jnrzj",
                  "uuid": uuid}
    response_login = requests.post(url=sso_login, data=json.dumps(data_login), headers=headers)
    token = json.loads(response_login.text)['data']['token']
    return token


def send_storage_job(database, authorization, jar='saas-data-storage-1.1.0.jar', ip='192.168.2.102', port='8799'):
    url = ''
    if database == 'es':
        url = f"http://{ip}:{port}/oss/flink-job/deploy/storage-standard-log/es/{jar}"
    elif database == 'ck':
        url = f"http://{ip}:{port}/oss/flink-job/deploy/storage-standard-log/ck/{jar}"
    headers = {
        "Authorization": f"Bearer {authorization.strip()}",
        "Content-Type": "application/json"
    }
    response = requests.get(url=url, headers=headers)
    return response.text


def one_touch_send_storage_job(database, jar='saas-data-storage-1.1.0.jar', ip='192.168.2.102', port='8073'):
    #  默认是192.168.2.109:8073的数据存储任务
    try:
        token = ssoLogin(ip, port)
        res = send_storage_job(database, token, jar, ip, port)
        if json.loads(res)['r'] == 0:
            return 'send success'
        else:
            return 'send fail'
    except Exception as e:
        print(f"发生异常: {e}")


if __name__ == '__main__':
    result = one_touch_send_storage_job('ck', ip='192.168.104.47', port='8013')
    print(result)
